# define
SRCDIR = src
OBJDIR = _build
C_SRCS := $(wildcard $(SRCDIR)/*.c)  
CPP_SRCS := $(wildcard $(SRCDIR)/*.cpp) 
C_OBJS := $(C_SRCS:$(SRCDIR)/%.c=$(OBJDIR)/%.o)  
CPP_OBJS := $(CPP_SRCS:$(SRCDIR)/%.cpp=$(OBJDIR)/%.o)   

# use pkg-config for getting CFLAGS and LDLIBS
FFMPEG_LIBS=    libavdevice                        \
                libavformat                        \
                libavfilter                        \
                libavcodec                         \
                libswresample                      \
                libswscale                         \
                libavutil                          \

CFLAGS += -Wall -g
CFLAGS := $(shell pkg-config --cflags $(FFMPEG_LIBS)) $(CFLAGS)
CXXFLAGS := $(CFLAGS) -std=c++11 
LDLIBS := $(shell pkg-config --libs $(FFMPEG_LIBS)) $(LDLIBS)
LDLIBS += -lm

# build targets
TARGET=     run-ffmpeg

.phony: all clean

all: $(TARGET)
	@echo "SRCS is $(C_SRCS) $(CPP_SRCS)"
	@echo "TARGET is $(TARGET)"

# link
$(TARGET): $(C_OBJS) $(CPP_OBJS)  
	g++ -o $@ $^ $(LDLIBS) 

# build c
$(OBJDIR)/%.o: $(SRCDIR)/%.c  
	@mkdir -p $(OBJDIR)  
	gcc $(CFLAGS) -c -o $@ $<  

# build cpp
$(OBJDIR)/%.o: $(SRCDIR)/%.cpp  
	@mkdir -p $(OBJDIR)  
	g++ $(CXXFLAGS) -c -o $@ $<  

clean: 
	$(RM) $(TARGET)
	$(RM) -r $(OBJDIR)
	$(RM) output*
